<template>
  <div>
    <!-- 订单管理 -->

    <!-- 搜索框 -->

    <el-card>
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-input
            placeholder="搜索"
            v-model="pager.keyword"
            style="width: 450px"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="search"
            >搜索</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>

    <!-- <div>
      <el-table :data="tableData" border style="width: 100%">
    表格内容水平居中   :cell-style="{ textAlign: 'center' }"
    </div> -->

    <el-table
      :data="tableData"
      border
      style="width: 100%"
      :header-cell-style="{ textAlign: 'center' }"
      :cell-style="{ textAlign: 'center' }"
    >
      <el-table-column fixed prop="id" label="Id" width="120">
      </el-table-column>
      <el-table-column prop="productName" label="商品名称" width="120">
      </el-table-column>
      <el-table-column prop="propertyValue" label="参数" width="180">
      </el-table-column>
      <el-table-column prop="orderCode" label="订单号" width="300">
      </el-table-column>
      <el-table-column prop="total" label="单价" width="120">
      </el-table-column>
      <el-table-column prop="number" label="数量" width="120">
      </el-table-column>
      <!-- <el-table-column prop="isDeleted" label="是否删除" width="130">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.isDeleted ? 'danger' : 'primary '"
            size="small"
            >{{ scope.row.isDeleted ? "是" : "否" }}</el-tag
          >
        </template>

      </el-table-column> -->
      <el-table-column prop="isActived" label="状态" width="130">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isActived"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="changeSwitchs(scope.$index, scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="createdTime" label="订单创建时间" width="350">
      </el-table-column>
      <el-table-column fixed="right" prop="operation" label="操作" width="140">
        <template slot-scope="scope">
          <!-- <el-button
            @click="handleClick(scope.$index, scope.row)"
            type="text"
            size="small"
            icon="el-icon-edit"
            >编辑</el-button
          > -->
          <el-button
            type="text"
            size="small"
            icon="el-icon-delete"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->

    <div class="block">
      <el-pagination
        @size-change="handlePageSizeChange"
        @current-change="handlePageIndexChange"
        :current-page="pager.pageIndex"
        :page-size="pager.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.rowsTotal"
        background
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getOrder, deleOrder } from '../../api/order'
import formatTime from '../../utils/formatTime'
// import getColor from '../../utils/txorbg'

export default {
  mounted () {
    setTimeout(() => {
      console.log(this.tableData)
    }, 1500)
    this.fetchData(this.pager)
  },
  data () {
    return {
      formLabelWidth: '120px',
      currentIndex: 0,
      dialogFormVisible: false,
      labelPosition: 'left',
      pager: {
        pageIndex: 1,
        pageSize: 10,
        rowsTotal: 30,
        keyword: ''
      },
      //   复选框  角色属性
      options: [],
      value: '',
      tableData: [

      ],
      formData: {
        id: 0,
        productName: '',
        propertyValue: '',
        total: '',
        orderCode: '',
        number: ''
      }
    }
  },
  methods: {
    // 删除用户
    async handleDelete (index, row) {
      // 删除时获得当前数组的下标
      this.currentIndex = index
      row.isDeleted = true

      const res = await deleOrder(row.id, row)
      if (res.data.code === 200) {
        this.$message.success('删除成功')
      } else {
        row.isDeleted = false
        this.$message.error('删除失败')
      }
    },

    // 搜索
    search () {
      this.pager.pageIndex = 1
      // this.pager. = 10
      this.fetchData(this.pager)
      // this.$message.success('查找数据成功！')
    },

    // switch开关
    async changeSwitchs (index, row) {
      console.log(index, row)
      const res = await deleOrder(row.id, row)
      console.log(res)
      if (res.data.code === 200) {
        this.$message.success('修改成功')
      } else {
        this.$message.error('修改失败')
      }
    },

    // 当页面大小发生改变的时候，重新拉取数据
    handlePageSizeChange (val) {
      console.log(val)
      this.pager.pageSize = val
      this.fetchData(this.pager)
    },
    // 当页面代码发生改变的时候，重新拉取数据
    handlePageIndexChange (val) {
      console.log(val)
      this.pager.pageIndex = val
      this.fetchData(this.pager)
    },

    // 拉取后台数据并且带上分页
    fetchData (pager) {
      getOrder(pager).then((data) => {
        data.data.data.data.forEach((item) => {
          item.createdTime = formatTime(item.createdTime)
        })

        console.log(data.data.data.data)
        this.tableData = data.data.data.data
        const pager = data.data.data.pager
        pager.keyword = this.pager.keyword
        this.pager = pager
      })
    }
  }
}
</script>
<style>
.block {
  text-align: center;
  /* display: flex; */
  margin-top: 15px;
}
</style>
